﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VAELinker.Utils;

namespace VAELinker.Entry
{
    public class EntryVAE : EntryBase
    {
        private bool linking = false;

        public EntryModel LinkedModel { get; private set; }
        public bool IsSymLink { get; private set; }
        public string VAEName { get; private set; }
        public EntryVAE(string fullName, string root) : base(fullName, root, FileType.Vae)
        {
            if (SymLinkUtil.TryFileIsReparsePoint(fullName, out bool result))
            {
                IsSymLink = result;
                if (IsSymLink && SymLinkUtil.TryGetFinalPathName(fullName, out string vaePath))
                {
                    VAEName = Path.GetFileName(vaePath);
                }
            }
        }

        public void Link(EntryModel model)
        {
            if (!linking)
            {
                linking = true;
                if(model == null)
                {
                    LinkedModel?.Link(null);
                    LinkedModel = null;
                }
                else
                {
                    LinkedModel = model;
                    model.Link(this);
                }
                linking = false;
            }
        }

        public bool MatchesModel(EntryModel entryModel)
        {
            return $"{PathName}{FileNameNoExtension}".Equals($"{entryModel.PathName}{entryModel.FileNameNoExtension}.vae", StringComparison.InvariantCultureIgnoreCase);
        }
    }
}
